package com.goparry.taskman.repository;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TaskNote implements Comparable<TaskNote>{
	static SimpleDateFormat FORMATTER_SET = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static SimpleDateFormat FORMATTER_GET = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private String taskName;
	private String name;
	private String author;
	private String type;
	private Date dateMofified;
	private String key;
	private String textNote;
	private String localUri;
	private long length;
	public static SortBy sortBy;
	public enum SortBy { NAME, DATE, TYPE }
		
	// Task Constructor to explicitly create a new Task
	public TaskNote(String name, String author, String type, Date dateMofified, String key, long length) {
		super();
		this.name = name;
		this.author = author;
		this.type = type;
		this.dateMofified = dateMofified;
		this.key = key;
		this.length = length;
	}
	public TaskNote(String name, String author, String type, String textNote, Date dateMofified) {
		super();
		this.name = name;
		this.author = author;
		this.type = type;
		this.dateMofified = dateMofified;
		this.textNote = textNote;
	}
	public TaskNote(String taskName, String name, String author, String type, String LocalUri) {
		super();
		this.taskName = taskName;
		this.name = name;
		this.author = author;
		this.type = type;
		this.localUri = LocalUri;
	}
	
	public TaskNote() {
		super();
	}
	
	public TaskNote copy() {
		if (key.length()==0) {
			return new TaskNote(getName(), getAuthor(), getType(), getTextNote(), getDateMofified());
		} else {
			return new TaskNote(getName(), getAuthor(), getType(), getDateMofified(), getKey(), getLength());
		}
	}
	@Override
	public String toString() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
	public String getTaskName() {
		return taskName;
	}
	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}
	public String getLocalUri() {
		return localUri;
	}
	public void setLocalUri(String localUri) {
		this.localUri = localUri;
	}
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getDateMofified() {
		return dateMofified;
	}
	public String getDateMofifiedAsString() {
		return FORMATTER_GET.format(dateMofified);
	}
	public void setDateMofified(Date dateMofified) {
		this.dateMofified = dateMofified;
	}
	public void setDateMofified(String dateMofified) {
		try {
			this.dateMofified = FORMATTER_SET.parse(dateMofified.trim());
		} catch (ParseException e) {
			this.dateMofified = null;
		}
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getTextNote() {
		return textNote;
	}

	public void setTextNote(String textNote) {
		this.textNote = textNote;
	}

	public long getLength() {
		return length;
	}
	public void setLength(long length) {
		this.length = length;
	}
	public SortBy getSortBy() {
		return sortBy;
	}
	public void setSortBy(SortBy sortBy) {
		TaskNote.sortBy = sortBy;
	}
	public int compareTo(TaskNote another) {
		switch (sortBy) {
		case NAME:
			return name.compareToIgnoreCase(another.getName());
		case DATE:
			return dateMofified.compareTo(another.getDateMofified());
		case TYPE:
			return type.compareToIgnoreCase(another.getType());
		default:
			return 0;
		}
	}


}
